{% extends "base.html" %}
{% block ext_title %}
    <title>图书信息新增</title>
{% endblock %}
{% block content %}
    <div class="content-wrapper">
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-12">
                        <ol class="breadcrumb float-sm-left">
                            <li class="breadcrumb-item"><a href="{{url_for('book_list')}}">图书管理</a></li>
                            <li class="breadcrumb-item active">图书增加</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title">图书信息增加</h3>
                            </div>
                            <form class="form-horizontal"  enctype="multipart/form-data"  method="post" novalidate>
                                {{ form.csrf_token }}
                                <div class="card-body">
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">图书名称</label>

                                        <div class="col-sm-10">
                                            {{ form.name }}
                                            <span style="color:red">{{ form.name.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">ISBN</label>
                                        <div class="col-sm-10">
                                            {{ form.isbn }}
                                            <span style="color:red">{{ form.isbn.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">图书封面</label>
                                        <div class="col-sm-10">
                                            <div class="input-group">
                                                <div class="custom-file">
                                                    {{ form.photo }}
                                                    <label class="custom-file-label">选择图书封面</label>
                                                </div>
                                            </div>
                                            <img id="preview-image" src="{{ url_for('static',filename='img/default-150x150.png') }}"
                                                 style="width: 192px; height: 192px;"/>
                                            <span style="color:red">{{ form.photo.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">价格</label>
                                        <div class="col-sm-10">
                                            {{ form.price }}
                                            <span style="color:red">{{ form.price.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">出版社</label>
                                        <div class="col-sm-10">
                                            {{ form.publish }}
                                            <span style="color:red">{{ form.publish.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">作者</label>
                                        <div class="col-sm-10">
                                            {{ form.author }}
                                            <span style="color:red">{{ form.author.errors[0] }}</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="card-footer">
                                    <button type="submit" class="btn btn-info">保存</button>
                                    <script>    var bookListUrl = "{{ url_for('book_list') }}";
                                    </script>
                                    <button type="button" class="btn btn-default"
                                            onclick="javascript:window.location=bookListUrl">返回
                                    </button>
{#                                    <span style="color:red">{{ info }}</span>#}
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
<script>
document.addEventListener('DOMContentLoaded', function () {
    var input = document.querySelector('.custom-file-input');
    var label = document.querySelector('.custom-file-label');
    var preview = document.getElementById('preview-image');
    if(input && label){
        input.addEventListener('change', function(e){
            var fileName = e.target.files[0] ? e.target.files[0].name : "选择图书封面";
            label.textContent = fileName;
            // 图片预览
            if (e.target.files[0] && preview) {
                var reader = new FileReader();
                reader.onload = function(evt) {
                    preview.src = evt.target.result;
                }
                reader.readAsDataURL(e.target.files[0]);
            }
        });
    }
});
</script>
{% endblock %}